package com.train.system.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.resp.PageResp;
import com.train.mybatis.base.MyBaseMapper;
import com.train.system.dto.seat.TrainSeatPageDTO;
import com.train.system.po.TrainSeatPO;
import com.train.system.resp.seat.TrainSeatPageResp;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【trainSeat(座位)】的数据库操作Mapper
 * @createDate 2025-02-20 23:20:20
 * @Entity generator.domain.TrainSeat
 */
public interface TrainSeatMapper extends MyBaseMapper<TrainSeatPO> {

    default PageResp<TrainSeatPageResp> pageList(TrainSeatPageDTO trainSeatPageDTO) {
        LambdaQueryWrapper<TrainSeatPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(trainSeatPageDTO.getCode())) {
            queryWrapper.like(TrainSeatPO::getTrainCode, trainSeatPageDTO.getCode());
        }
        queryWrapper.orderBy(true, true, TrainSeatPO::getTrainCode)
                .orderBy(true, true, TrainSeatPO::getCarriageIndex)
                .orderBy(true, true, TrainSeatPO::getCarriageSeatIndex);
        PageResp<TrainSeatPO> trainSeatPOPageResp = this.selectPage(trainSeatPageDTO, queryWrapper);
        List<TrainSeatPageResp> trainSeatPageRespList = BeanUtil.copyToList(trainSeatPOPageResp.getData(), TrainSeatPageResp.class);
        return new PageResp<>(trainSeatPOPageResp.getTotal(), trainSeatPageRespList);
    }

    default boolean removeSeatByTrainCode(String trainCode) {
        int delete = this.delete(new LambdaQueryWrapper<TrainSeatPO>().eq(TrainSeatPO::getTrainCode, trainCode));
        return delete > 0;
    }

    default boolean existsSeatByTrainCode(String trainCode) {
        return this.exists(new LambdaQueryWrapper<TrainSeatPO>().eq(TrainSeatPO::getTrainCode, trainCode));
    }

    default List<TrainSeatPageResp> getTrainSeatListByTrainCodes(List<String> trainCodes) {
        LambdaQueryWrapper<TrainSeatPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainSeatPO::getTrainCode, TrainSeatPO::getSeatType, TrainSeatPO::getCarriageIndex,
                TrainSeatPO::getRow, TrainSeatPO::getCol, TrainSeatPO::getCarriageSeatIndex);
        queryWrapper.in(TrainSeatPO::getTrainCode, trainCodes);
        return BeanUtil.copyToList(this.selectList(queryWrapper), TrainSeatPageResp.class);
    }
}